//
//  ProductSKUCell.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/12.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "ProductSKUCell.h"
#import "SKUCollectionCell.h"

@interface ProductSKUCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *skuCollectionView;

@property (nonatomic, strong) NSMutableArray <ProductSKUModel *>*skuArray;

@end

@implementation ProductSKUCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.titleLab];
    [self addSubview:self.skuCollectionView];
    
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
    }];
    
    [self.skuCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(-10);
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(10);
    }];
    
}

- (void)fillCellWithTitle:(NSString *)title Array:(NSMutableArray<ProductSKUModel *> *)array {
    self.titleLab.text = title;
    self.skuArray = array;
    [self.skuCollectionView reloadData];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SKUCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SKUCollectionCellID" forIndexPath:indexPath];
    [cell fillCellWithMode:self.skuArray[indexPath.row]];
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.skuArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(120, 30);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        if (self.skuClickBlock) {
            self.skuClickBlock(indexPath.row);
        }
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.text = @"品类";
        _titleLab.font = [UIFont systemFontOfSize:16];
    }
    return _titleLab;
}

- (UICollectionView *)skuCollectionView {
    if (!_skuCollectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _skuCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _skuCollectionView.delegate = self;
        _skuCollectionView.dataSource = self;
        _skuCollectionView.showsHorizontalScrollIndicator = NO;
        _skuCollectionView.showsVerticalScrollIndicator = NO;
        _skuCollectionView.backgroundColor = UIColorHex(0xFFFFFF);
        [_skuCollectionView registerNib:[UINib nibWithNibName:@"SKUCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"SKUCollectionCellID"];
    }
    
    return _skuCollectionView;
}




//- (void)btnOnClicked:(UIButton *)btn {
//
//    if (self.skuClickBlock) {
//        self.skuClickBlock(btn.tag);
//    }
//}

@end
